package com.etc.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.HistoryInfo;
import com.etc.entity.MedicalHistory;
import com.etc.entity.OrderInfo;
import com.etc.entity.Patient;
import com.etc.entity.Prescription;
import com.etc.service.MedicalHistoryService;
import com.etc.service.OrderInfoService;
import com.etc.service.PatientService;

@Controller
public class PatientController {

	@Resource
	private PatientService patientService;
	@Resource
	private OrderInfoService orderInfoService;
	@Resource
	private MedicalHistoryService medicalHistoryService;
	public static List<HistoryInfo> listMedicineInfo;
	
	@RequestMapping(value = "getPatient")
	public String getPatient(Model model) {
		model.addAttribute("patient", patientService.selectPatient());
		return "forward:/back/user-management.jsp";
	}
	
	@RequestMapping(value="selPatientByTel1/{patientTel}",method=RequestMethod.GET)
	@ResponseBody
	public String   selPatientByTel1(@PathVariable(value="patientTel") String patientTel,Model model) {
		//根据Id查询病人
		Patient patient = patientService.selPatientById(patientTel);
		
		
		model.addAttribute("patient", patient);
		
		if(patient==null) {
			return "true";
		}
		else {
			return "false";
		}
	}
	/**ajax
	 * 插入病人信息
	 * @param patientTel
	 * @param patient
	 * @param model
	 * @return
	 */
	@RequestMapping(value="insertPatient",method=RequestMethod.POST)
	@ResponseBody
	public boolean insertPatient(@RequestBody Patient patient,Model model) {
		
		boolean flag = patientService.insert(patient);
		model.addAttribute("flag",flag);
		return flag;
	}
	/**
	 * 根据Id查询病人信息
	 * @param patientId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="selPatientById/{patientTel}",method=RequestMethod.GET)
	public String  selPatientById(@PathVariable(value="patientTel") String patientTel,@RequestParam(value="page",required=true,defaultValue="0")int page,@RequestParam(value="pageSize",required=true,defaultValue="1")int pageSize,Model model) {
		
		
		//根据Id查询病人
		Patient patient = patientService.selPatientById(patientTel);
		//根据号码查
		List<HistoryInfo> listhistoryInfo=patientService.selPatientInfoByTel(patientTel,page,pageSize);
		int listhistoryInfoSize=patientService.selPatientInfoByTel(patientTel,0,100).size();
		
		List<OrderInfo> listOrderInfo = orderInfoService.selOrderInfoByTel(patientTel);
		
		model.addAttribute("patient", patient);
		model.addAttribute("listhistoryInfo",listhistoryInfo);
		model.addAttribute("listhistoryInfoSize",listhistoryInfoSize);
		model.addAttribute("page",page+1);
		model.addAttribute("listOrderInfo", listOrderInfo);
		return "forward:/front/personalinfo.jsp";
	}
	/**
	 * ajax更新病人信息
	 * @param patientId
	 * @param patient
	 * @param model
	 * @return
	 */
	@RequestMapping(value="updatePatient/{patientId}",method=RequestMethod.PUT)
	@ResponseBody
	public boolean updatePatient(@PathVariable(value="patientId") int patientId,@RequestBody Patient patient,Model model) {
		boolean flag = patientService.updatePatient(patient);
		model.addAttribute("flag",flag);
		return flag;
	}
	
	/**
	 * 医生根据手机号查询病人信息
	 * @param patientTel
	 * @param model
	 * @return
	 */
	@RequestMapping(value="selPatientByTel/{patientTel}",method=RequestMethod.GET)
	@ResponseBody
	public List<MedicalHistory> selPatientByTel(@PathVariable(value="patientTel") String patientTel,Model model) {
		return patientService.selectByPatientTel(patientTel);//根据手机号查询病人
	}
	
	/**
	 * 
	 * @param orderTime
	 * @param patient
	 * @param model
	 * @return
	 */
	@RequestMapping(value="delOrderByTime/{orderTime}",method=RequestMethod.DELETE)
	@ResponseBody
	public boolean delOrderByTime(@PathVariable(value="orderTime") String orderTime,@RequestBody Patient patient,Model model) {
		
		boolean flag = orderInfoService.delOrderByTime(patient.getPatienttel(), orderTime);
		
		return flag;
	}
	/**
	 * 医生查看药方
	 * @param patientSituationId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="selMedicine/{patientSituationId}",method=RequestMethod.GET)
	@ResponseBody
	public List<Prescription> selPatientByTel(@PathVariable(value="patientSituationId") int patientSituationId) {
		return patientService.getPrescription(patientSituationId);
	}
	/**
	 * 查询药品信息通过
	 * @param patientTel
	 * @param date
	 * @param model
	 * @return
	 */
	@RequestMapping(value="selMedicineInfoByDate/{patientTel}/{date}",method=RequestMethod.GET)
	@ResponseBody
	public String selMedicineInfoByDate(@PathVariable(value="patientTel") String  patientTel,@PathVariable(value="date") String date,Model model) {
		
		listMedicineInfo =patientService.selMedicineByDate(patientTel, date);
		
		return "";
	}
	//分页操作
	@RequestMapping(value="selInfo/{patientTel}/{page}/{pageSize}",method=RequestMethod.GET)
	public String  selInfo(@PathVariable(value="patientTel") String patientTel,@PathVariable(value="page")int page,@PathVariable(value="pageSize")int pageSize,Model model) {
		
		int startPage=(page-1)*pageSize;//从第几条信息开始显示
		
		//根据Id查询病人
		Patient patient = patientService.selPatientById(patientTel);
		//根据号码查
		List<HistoryInfo> listhistoryInfo=patientService.selPatientInfoByTel(patientTel,startPage,pageSize);
		//查询到信息的总数
		int listhistoryInfoSize=patientService.selPatientInfoByTel(patientTel,0,100).size();
		
		List<OrderInfo> listOrderInfo = orderInfoService.selOrderInfoByTel(patientTel);
		//病人信息
		model.addAttribute("patient", patient);
		//历史诊断信息
		model.addAttribute("listhistoryInfo",listhistoryInfo);
		//历史诊断信息数
		model.addAttribute("listhistoryInfoSize",listhistoryInfoSize);
		//页码
		model.addAttribute("page",page);
		//医生预约信息
		model.addAttribute("listOrderInfo", listOrderInfo);
		
		return "forward:/front/personalinfo.jsp";
	}
	//文件上传
	/*@RequestMapping(value="fileupload",method=RequestMethod.POST)
	public String upload(HttpServletRequest request,@RequestParam MultipartFile[] myfiles) {
	// 将request 转换为 MultipartHttpServletRequest
			// MultipartHttpServletRequest mhr = (MultipartHttpServletRequest) request;
			// 得到上传的文件
			// MultipartFile file=mhr.getFile("myfile");
			// 得到文件域的名字file.getName() 得到文件名file.getOriginalFilename()
			// System.out.println(file.getName()+","+file.getOriginalFilename());
			// 上传的目标路径

			for (MultipartFile file : myfiles) {
				// 得到文件的contentType
				System.out.println("contentType :" + file.getContentType());
				// 得到文件域的名字file.getName() 得到文件名file.getOriginalFilename()
				System.out.println(file.getName() + "," + file.getOriginalFilename());
				if(!file.getOriginalFilename().isEmpty()) {
				String path = request.getRealPath("/imgs") + "/"
						+ file.getOriginalFilename();
				System.out.println("path"+path);
			
				File destFile = new File(path);
				// 直接使用封装好的 copyInputStreamToFile 实现文件的上传功能
				try {
					FileUtils.copyInputStreamToFile(file.getInputStream(), destFile);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			}

			return "ss";
}*/
	
}
